{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "blind-lesbian", "metadata": {}, "outputs": [], "source": [ "# This short notebook will show how to take symbolic integrals in Python.\n", "# We will require the 'SymPy' module.\n", "import sympy as sym" ] }, { "cell_type": "code", "execution_count": 2, "id": "opponent-concord", "metadata": {}, "outputs": [], "source": [ "# First, let's define a symbol x.\n", "x = sym.Symbol('x')" ] }, { "cell_type": "code", "execution_count": 3, "id": "tropical-senegal", "metadata": {}, "outputs": [], "source": [ "# Then we can define f in terms of x.\n", "f = 2*x**2+3" ] }, { "cell_type": "code", "execution_count": 4, "id": "formal-holder", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2(2)^2 + 3 = 11\n" ] } ], "source": [ "# To evaluate f at a particular value of x, we can use 'subs()'.\n", "z = f.subs(x, 2)\n", "print('2(2)^2 + 3 =', z)" ] }, { "cell_type": "code", "execution_count": 5, "id": "outer-release", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{2 x^{3}}{3} + 3 x$" ], "text/plain": [ "2*x**3/3 + 3*x" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# To integrate f with respect to x, we use 'integrate()'.\n", "intf = f.integrate(x)\n", "intf" ] }, { "cell_type": "code", "execution_count": 6, "id": "coordinated-distribution", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{2 x^{3}}{3} + 3 x$" ], "text/plain": [ "2*x**3/3 + 3*x" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can get the same result using 'integrate()' in the following way:\n", "intf = sym.integrate(f, x)\n", "intf" ] }, { "cell_type": "code", "execution_count": 7, "id": "general-theme", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 27$" ], "text/plain": [ "27" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The above examples are indefinite integrals of f. We can evaluate\n", "# definite integrals using the same 'integrate()' function. We just\n", "# need to pass the limits of integration...\n", "intf = sym.integrate(f, (x, 0, 3))\n", "intf" ] }, { "cell_type": "code", "execution_count": 8, "id": "instrumental-rider", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 27$" ], "text/plain": [ "27" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# or:\n", "intf = f.integrate((x, 0, 3))\n", "intf" ] }, { "cell_type": "code", "execution_count": 10, "id": "retired-variety", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle - \\frac{2 a^{3}}{3} - 3 a + \\frac{2 b^{3}}{3} + 3 b$" ], "text/plain": [ "-2*a**3/3 - 3*a + 2*b**3/3 + 3*b" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# You can also used symbols for the integration limits.\n", "a = sym.Symbol('a')\n", "b = sym.Symbol('b')\n", "intf = f.integrate((x, a, b))\n", "intf" ] }, { "cell_type": "code", "execution_count": 11, "id": "honest-douglas", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle x \\sin{\\left(x y \\right)}$" ], "text/plain": [ "x*sin(x*y)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can make our functions more complicated. Here's a function of two variables.\n", "y = sym.Symbol('y')\n", "g = x*sym.sin(x*y)\n", "g" ] }, { "cell_type": "code", "execution_count": 12, "id": "joint-ethernet", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\begin{cases} - \\frac{x \\cos{\\left(x y \\right)}}{y} + \\frac{\\sin{\\left(x y \\right)}}{y^{2}} & \\text{for}\\: y \\neq 0 \\\\0 & \\text{otherwise} \\end{cases}$" ], "text/plain": [ "Piecewise((-x*cos(x*y)/y + sin(x*y)/y**2, Ne(y, 0)), (0, True))" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Here's the x-integral of g...\n", "g.integrate(x)" ] }, { "cell_type": "code", "execution_count": 13, "id": "intense-elevation", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle x \\left(\\begin{cases} - \\frac{\\cos{\\left(x y \\right)}}{x} & \\text{for}\\: x \\neq 0 \\\\0 & \\text{otherwise} \\end{cases}\\right)$" ], "text/plain": [ "x*Piecewise((-cos(x*y)/x, Ne(x, 0)), (0, True))" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# and here's the y-integral.\n", "g.integrate(y)" ] }, { "cell_type": "code", "execution_count": 14, "id": "thrown-large", "metadata": {}, "outputs": [], "source": [ "# Notice that the results of both of the g integrals depend on whether or not\n", "# x and y are zero. We can specify that our variables are non-zero when they\n", "# are first declared.\n", "x = sym.Symbol('x', nonzero = True)\n", "y = sym.Symbol('y', nonzero = True)\n", "g = x*sym.sin(x*y)" ] }, { "cell_type": "code", "execution_count": 15, "id": "biological-nirvana", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-x*cos(x*y)/y + sin(x*y)/y**2, -cos(x*y))" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Now the solutions of our integrals have no conditions.\n", "g.integrate(x), g.integrate(y)" ] }, { "cell_type": "code", "execution_count": 16, "id": "compliant-production", "metadata": {}, "outputs": [], "source": [ "# Alternatively, you can define Python functions.\n", "def fcn(x):\n", " return 2*x**2 + 3" ] }, { "cell_type": "code", "execution_count": 17, "id": "dietary-department", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{2 x^{3}}{3} + 3 x$" ], "text/plain": [ "2*x**3/3 + 3*x" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# These functions can be integrated in the same way. Here's the indefinite\n", "# integral...\n", "fcn(x).integrate(x)" ] }, { "cell_type": "code", "execution_count": 18, "id": "suited-bunny", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle - \\frac{2 a^{3}}{3} - 3 a + \\frac{2 b^{3}}{3} + 3 b$" ], "text/plain": [ "-2*a**3/3 - 3*a + 2*b**3/3 + 3*b" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ... and here's the indefinite integral.\n", "sym.integrate(fcn(x), (x, a, b))" ] }, { "cell_type": "code", "execution_count": 19, "id": "welsh-disorder", "metadata": {}, "outputs": [], "source": [ "# Of course, you can have functions of multiple variables.\n", "def gcn(x, y):\n", " return x*sym.sin(x*y)" ] }, { "cell_type": "code", "execution_count": 20, "id": "weighted-drink", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle - \\frac{\\sin{\\left(x y \\right)}}{y}$" ], "text/plain": [ "-sin(x*y)/y" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Here's the indefinite y-integral followed by the indefinite x-integral.\n", "z = sym.integrate(sym.integrate(gcn(x, y), y), x)\n", "z" ] }, { "cell_type": "code", "execution_count": 21, "id": "higher-auditor", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-sin(1) + sin(3)/3 - sin(6)/3 + sin(2)\n", "0.208004944104050\n" ] } ], "source": [ "# Here's the definite y-integral followed by the definite x-integral using\n", "# the other method of calling the 'integrate()' function.\n", "z = gcn(x, y).integrate((y, 1, 3)).integrate((x, -2, -1))\n", "print(z)\n", "print(sym.N(z))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }